package com.zhongwang.business.module.baseCostManage.controller;
import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.model.InputMachiningModel;
import com.zhongwang.business.module.baseCostManage.service.InputMachiningService;
import com.zhongwang.business.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *  控制层
 * @author China.kxx
 * @date 2020/10/17 13:50
 */
@Slf4j
@Controller
@RequestMapping("/dataEntry/machiningManHour")
public class InputMachiningController extends BaseController {


    //页面文件路径
    private String prefix = "/dataEntry";
    @Autowired
    private InputMachiningService inputMachiningService;
    //加载页面
    @GetMapping("/manpower")
    public String manpower(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/manpower";
    }


    @GetMapping
    public String page(Model model) {
        model.addAttribute("categories", inputMachiningService.findTypes());
        return "dataEntry/machiningManHour";
    }


    /**
     * 加载机加工时列表数据
     * @param inputMachiningModel
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult manpower(InputMachiningModel inputMachiningModel) {
        List<InputMachiningModel> list = inputMachiningService.inputMachiningHour(inputMachiningModel);
        return jsonResult(list);
    }
    /**
     * 导入
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/import")
    public JsonResult imputMachiningHour(@RequestParam("file") MultipartFile file, InputMachiningModel inputMachiningModel) {
        return success(inputMachiningService.importFile(file, inputMachiningModel));
    }
    /**
     * 错误文件导出
     * @param url 文件路径
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/exist")
    public JsonResult checkFileExist(String url) {
        return jsonResult(inputMachiningService.exist(url));
    }
    /**
     * 机加工时数据清空
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delMachining")
    public JsonResult delMachining(InputMachiningModel inputMachiningModel) {
        int row = inputMachiningService.delMachining(inputMachiningModel);
        if (row == -1){
            return error("机加工时数据已被接收，无法进行删除！！需联系商务科");
        }else if(row == 0){
            return error("机加工时无数据，操作失败");
        }else if(row == 1){
            return jsonResult(inputMachiningService.deleteManchining(inputMachiningModel));
        }else {
            return JsonResult.fail();
        }

//        jsonResult(row);

    }

}